package com.tsingteng.cosfun.ui.message.comment.usercomment;

import android.content.DialogInterface;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.app.AlertDialog;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.tsingteng.cosfun.R;
import com.tsingteng.cosfun.bean.AttentFansbean;
import com.tsingteng.cosfun.bean.Bean;
import com.tsingteng.cosfun.bean.ParagraphComment;
import com.tsingteng.cosfun.bean.PlayCommont;
import com.tsingteng.cosfun.bean.PullBlackBean;
import com.tsingteng.cosfun.bean.UserCommentResultBean;
import com.tsingteng.cosfun.common.Navigate;
import com.tsingteng.cosfun.ui.cosfun.AtFragment;
import com.tsingteng.cosfun.ui.message.geneal.PlayDetailsAdapter;
import com.tsingteng.cosfun.ui.mine.HomeMineFragment;
import com.tsingteng.cosfun.ui.mine.TerminalActivity;
import com.tsingteng.cosfun.utils.KeyBoardUtils;
import com.tsingteng.cosfun.utils.LogonUtils;
import com.tsingteng.cosfun.utils.SPUtils;
import com.tsingteng.cosfun.utils.ToastUtils;
import com.tsingteng.cosfun.widget.dialog.BaseBottomSheetFragment;
import com.tsingteng.cosfun.widget.dialog.BottomListDialog;
import com.tsingteng.cosfun.widget.dialog.CommentPlayDialog;

import java.util.ArrayList;
import java.util.List;

/**
 * @我。赞我。评论我页面点击展示段子评论页 常胜
 * 2018.8.23
 */
public class UserCommentFragment extends BaseBottomSheetFragment implements UserCommentContract.IUserCommentView,  BaseQuickAdapter.OnItemClickListener, PlayDetailsAdapter.OnPlayDetailClickListener {

    private int page = 1;
    private String reply_id = "0";
    private boolean isReply = false;
    private PlayCommont.CommontList item;
    private TextView greatNum;
    private ImageView imageView;
    private UserCommentPresenter userCommentPresenter;
    private PlayDetailsAdapter userCommentAdapter;
    private TextView user_comm_count;
    private RelativeLayout top_rel;
    private RecyclerView user_comm_recycler;
    private EditText user_comm_et;
    private TextView user_comm_at;
    private RelativeLayout rela;
    private TextView user_comm_empty;
    private int workId;
    private List<Bean.DataBeanX.DataBean> mList = new ArrayList<>();
    private int commentId;
    private int rid;
    private ImageView ivClose;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    public static UserCommentFragment newInstance(int id) {
        UserCommentFragment userCommentFragment = new UserCommentFragment();
        Bundle bundle = new Bundle();
        bundle.putInt("id", id);
        userCommentFragment.setArguments(bundle);
        return userCommentFragment;
    }


    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_user_comment, container, false);
        initView(view);
        workId = getArguments().getInt("wid");
        rid = getArguments().getInt("rid");
         userCommentAdapter = new PlayDetailsAdapter();
        user_comm_recycler.setLayoutManager(new LinearLayoutManager(getContext()));
        user_comm_recycler.setAdapter(userCommentAdapter);
        userCommentAdapter.setOnPlayDetailClickListener(this);
        userCommentPresenter = new UserCommentPresenter(this);
        userCommentPresenter.getPlayCommentList(workId,page ,rid==0?"":String.valueOf(rid));
        userCommentAdapter.setOnItemClickListener(this);
        ivClose.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });
        user_comm_et.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                isReply = false;
               showCommentDialog("",null);
            }
        });
        user_comm_at.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
              /*  String userInput = user_comm_at.getText().toString().trim();
                if (TextUtils.isEmpty(userInput)) {
                    Toast.makeText(getContext(), "评论不能为空!", Toast.LENGTH_SHORT).show();
                    return;
                }
                if (isReply) {
                    userCommentPresenter.getMyPlayComment(workId, "1", commentId,userInput, reply_id);
                } else {
                    userCommentPresenter.getMyPlayComment(workId, "0", commentId, userInput, reply_id);
                }*/

                AtFragment atFragment = AtFragment.newInstance();
                atFragment.setOnItemSelectLisenter(new AtFragment.OnItemSelectLisenter() {
                    @Override
                    public void selected(AttentFansbean.Data data) {
                        showCommentDialog(String.valueOf(workId),data);
                        atFragment.dismiss();
                    }
                });
                if (null ==getActivity()){
                    return;
                }
                atFragment.show(getActivity().getSupportFragmentManager(), "myFragment");
            }
        });
        return view;
    }


    @Override
    public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
        PlayCommont.CommontList commontList = (PlayCommont.CommontList) adapter.getData().get(position);
         /*   this.id = commontList.getProfileId();
        commentId = commontList.getCommentId();
        if(SPUtils.init().getInt("profile_id") == commontList.getProfileId()){
            new AlertDialog.Builder(getContext()).setTitle("提示").setMessage("确认要删除此条评论吗？").setNegativeButton("确认", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialogInterface, int i) {
                    userCommentPresenter.loginDeleteComment(commentId);
                }
            }).setPositiveButton("取消",null).create().show();
        }else{
            user_comm_et.setText("");
            KeyBoardUtils.searchPoint(user_comm_et);
            user_comm_et.setText("回复" + commontList.getNickName() + ":");
            user_comm_et.setSelection(user_comm_et.getText().toString().trim().length());
            isReply = true;
        }*/

        final int index = position;
        if (commontList.getProfileId() == LogonUtils.getProFildId()) {
            List<String> list = new ArrayList<>();
            list.add("删除");
            BottomListDialog bottomListDialog = new BottomListDialog(getActivity());
            bottomListDialog.setList(list);
            bottomListDialog.setOnClickBottomListListener(new BottomListDialog.OnClickBottomListListener() {
                @Override
                public void onClickItem(BaseQuickAdapter adapter, View view, int position, List<String> list) {
                    userCommentAdapter.remove(index);
                    adapter.notifyDataSetChanged();
                    userCommentPresenter.loginDeleteComment(commontList.getCommentId());


                }

                @Override
                public void onClickBottom() {

                }
            });
            bottomListDialog.show();
        } else {
//            dialog = builder.setMessage(position, workId, commId, "").setItemClickListener(this).createDialog();
//            dialog.show();

            List<String> list = new ArrayList<>();
            list.add("回复");
            list.add("举报");
            BottomListDialog bottomListDialog = new BottomListDialog(getActivity());
            bottomListDialog.setList(list);
            bottomListDialog.setOnClickBottomListListener(new BottomListDialog.OnClickBottomListListener() {
                @Override
                public void onClickItem(BaseQuickAdapter adapter, View view, int position, List<String> list) {
                    if (position == 0) {
                        isReply = true;
                        showCommentDialog(String.valueOf(commontList.getCommentId()),null);

                    } else if (position == 1) {
                        int type = 3;
                        Navigate.startReportList(getContext(), type, commontList.getCommentId());
                    }
//                    deleteComm(index);
                }

                @Override
                public void onClickBottom() {

                }
            });
            bottomListDialog.show();
        }
    }

    @Override
    public void showCommentListResult(PlayCommont playCommont) {
        if(playCommont != null ){
            user_comm_count.setText("评论"+playCommont.getData().size());
            if(playCommont.getData() != null){
                userCommentAdapter.addData(playCommont.getData());
            }else{return;}
        }
    }

    @Override
    public void showMyPlayCommentResult(UserCommentResultBean userCommentResultBean) {
        this.dismiss();
    }

    @Override
    public void showPraiseResult(Integer data) {
            if (data==1) {
                item.setPraiseStatus(1);
                int num = Integer.valueOf(item.getPraiseCount()) + 1;
                item.setPraiseCount(num);
                greatNum.setText(String.valueOf(num));
                Toast.makeText(getContext(), "点赞成功!", Toast.LENGTH_SHORT).show();
                imageView.setImageDrawable(getContext().getResources().getDrawable(R.drawable.great_pink_small_icon));
            }else {
                int num = Integer.valueOf(item.getPraiseCount()) -1;
                item.setPraiseCount(num);
                greatNum.setText(String.valueOf(num));
                imageView.setImageDrawable(getContext().getResources().getDrawable(R.drawable.great_small_icon));
                Toast.makeText(getContext(), "取消点赞成功!!", Toast.LENGTH_SHORT).show();
                item.setPraiseStatus(0);
            }
    }

    @Override
    public void showDeleteCommentResult(PullBlackBean body) {
        Toast.makeText(getContext(), "删除成功！", Toast.LENGTH_SHORT).show();
    }

    @Override
    public void showAddCommentResult(ParagraphComment data) {
        isReply = false;
        userCommentAdapter.addData(0, data.getParagraphComment());
        userCommentAdapter.notifyDataSetChanged();
    }


    @Override
    public void showLoading(String msg) {

    }

    @Override
    public void hideLoading() {

    }

    @Override
    public void showFail(String msg) {

    }

    @Override
    public void showError() {

    }

    @Override
    public void showEmpty() {

    }


    private void initView(View view) {
        user_comm_count = (TextView) view.findViewById(R.id.user_comm_count);
        top_rel = (RelativeLayout) view.findViewById(R.id.top_rel);
        user_comm_recycler = (RecyclerView) view.findViewById(R.id.user_comm_recycler);
        user_comm_et = (EditText) view.findViewById(R.id.user_comm_et);
        user_comm_at = (TextView) view.findViewById(R.id.user_comm_at);
        rela = (RelativeLayout) view.findViewById(R.id.rela);
        user_comm_empty = (TextView) view.findViewById(R.id.user_comm_empty);
        ivClose = (ImageView) view.findViewById(R.id.iv_close);
    }


    @Override
    public void onPraiseClick(PlayCommont.CommontList item, ImageView mCreat, TextView mGreatNum) {
        this.imageView = mCreat;
        this.greatNum = mGreatNum;
        this.item = item;
        String commentId=String.valueOf(item.getCommentId());
        if (item.getPraiseStatus()==1) {
            userCommentPresenter.getPraiseOrNotData("-1",commentId,"1");
        }else {
            userCommentPresenter.getPraiseOrNotData("1",commentId,"1");
        }
    }

    @Override
    public void onUserClick(String userId) {
        Bundle bundle = new Bundle();
        bundle.putInt("user_id", Integer.parseInt(userId));
        bundle.putInt("tag", 1);
        TerminalActivity.showFragment(getActivity(), HomeMineFragment.class, bundle);
    }


    private void showCommentDialog(String commonId, AttentFansbean.Data data) {
        CommentPlayDialog dialog = new CommentPlayDialog(commonId, data, new CommentPlayDialog.CommentPlayDialogListener() {
            @Override
            public void commSuccess(String commStr) {
                if (isReply) {
                    userCommentPresenter.getAddPlayComment(String.valueOf(workId), "1", commStr, String.valueOf(commonId));
                } else {
                    userCommentPresenter.getAddPlayComment(String.valueOf(workId), "0", commStr, null);
                }
            }
        });
        dialog.show(getActivity().getSupportFragmentManager(), "comment");
    }
}
